function tasks.check_xp(map_id, needed_xp)
   if needed_xp <= 0 then
      lobby.team_win(map_id)
   end
end

function tasks.add_xp(pos, node, puncher, swap_to)
   local ghost = puncher:get_attribute('ghost')
   if ghost == 'false' then
      local meta = minetest.get_meta(pos)
      local timer_length = tonumber(meta:get_string('timer')) or 60
      local earned_xp = tonumber(meta:get_string('xp')) or 1
      local wield = puncher:get_wielded_item()
      local wield_name = wield:get_name()
      if wield_name == '' then
         local name = puncher:get_player_name()
         local timer = minetest.get_node_timer(pos)
         local map_id = lobby.game[name]
         local traitor = lobby.traitors[map_id]
         minetest.swap_node(pos, {name = swap_to, param2 = node.param2})
         local meta = minetest.get_meta(pos)
         meta:set_string('formspec', '')
         timer:start(timer_length)
         if map_id ~= 'lobby' then
            if name ~= traitor then
               local game_data = lobby.savedata.data[map_id]
               lobby.xp[map_id] = lobby.xp[map_id] + earned_xp
               local needed_xp = game_data['xp'] - lobby.xp[map_id]
               minetest.chat_send_player(name, 'You just earned '..earned_xp..' XP for your team.\nYou need '..needed_xp..' more XP to defeat the imposter.')
               tasks.check_xp(map_id, needed_xp)
            else
               minetest.chat_send_player(name, 'You\'re the traitor, you can do tasks, but you won\'t earn XP. Try killing the other players.')
            end
         end
      end
   end
end

function tasks.only_add_xp(xp, name) -- This function only adds XP, you are responsible for modifying the task node yourself.
   local player = minetest.get_player_by_name(name)
   local ghost = player:get_attribute('ghost')
   if ghost == 'false' then
      local map_id = lobby.game[name]
      local traitor = lobby.traitors[map_id]
      if map_id ~= 'lobby' then
         if name ~= traitor then
            local game_data = lobby.savedata.data[map_id]
            lobby.xp[map_id] = lobby.xp[map_id] + xp
            local needed_xp = game_data['xp'] - lobby.xp[map_id]
            minetest.chat_send_player(name, 'You just earned '..xp..' XP for your team.\nYou need '..needed_xp..' more XP to defeat the imposter.')
            tasks.check_xp(map_id, needed_xp)
         else
            minetest.chat_send_player(name, 'You\'re the traitor, you can do tasks, but you won\'t earn XP. Try killing the other players.')
         end
      end
   end
end

tasks.formspec_configuration =
   'size[6,3]'..
   'label[.5,.25;Please enter data in this format: \nXP, Timer_Duration, Task_name]'..
   'field[1,1.5;5,1;input;;]'..
   'button_exit[2,2;2,1;save;Submit]'
